package com.eeepay.eeepay_v2.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.eeepay.eeepay_v2.R;
import com.eeepay.v2_library.adapter.ABBaseAdapter;
import com.eeepay.v2_library.adapter.ABViewHolder;

import java.util.List;

import cn.eeepay.api.grpc.nano.MerchantSearchProto;

/**
 * Created by Ching on 2017/2/28.
 */

public class AddProfitAdapter2 extends ABBaseAdapter<MerchantSearchProto.AgentServicesInfo> {
    private List<MerchantSearchProto.AgentServicesInfo> list;
    private int index = -1;

    public AddProfitAdapter2(Context context, List<MerchantSearchProto.AgentServicesInfo> list) {
        super(context);
        this.list = list;
    }
    public AddProfitAdapter2(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_agent_add_profit_listview2;
    }

    @Override
    public void convert(final ABViewHolder holder, MerchantSearchProto.AgentServicesInfo model) {
//        string serviceId = 1;	// 服务id
//        string serviceName = 2;// 服务名称
//        string holidaysMark = 3;	// 节假日标志:1-只工作日，2-只节假日，0-不限
//        string cardType = 4;		// 银行卡种类:0-不限，1-只信用卡，2-只储蓄卡
//        string bpId = 5;			// 业务产品id
//        string bpName = 6;			// 业务产品名称
//        string serviceType = 7;		// 服务类型
//        string serviceType2 = 8;	// 主服务类型, 如果该服务是提现服务,则关联出主服务类型
//        string serviceTypeName = 9;	// 服务种类名称

        holder.setText(R.id.tv_pbName,model.bpName);//业务产品名称
        holder.setText(R.id.tv_serviceType,model.serviceTypeName);//服务种类名称
        String cardType = "";
        if (model.cardType.equals("0"))
            cardType = "不限";
        else if (model.cardType.equals("1"))
            cardType = "信用卡";
        else if (model.cardType.equals("2"))
            cardType = "储蓄卡";
        holder.setText(R.id.tv_cardType,cardType);//银行卡种类

        String holidaysMark = "";
        if (model.holidaysMark.equals("0"))
            holidaysMark = "不限";
        else  if (model.holidaysMark.equals("1"))
            holidaysMark = "工作日";
        else  if (model.holidaysMark.equals("2"))
            holidaysMark = "节假日";
        holder.setText(R.id.tv_holidaysMark,holidaysMark);//节假日标志


        //serviceType = 10000 or serviceType=10001 符号元
        String serviceType = model.serviceType;
        if (serviceType.equals("10000") || serviceType.equals("10001") )
            holder.setText(R.id.tv_mark,"元");//符号% 或者元
        else
            holder.setText(R.id.tv_mark,"%");//符号% 或者元
        //设置分润信息  et_shareData
        final EditText editText = holder.getView(R.id.et_shareData);
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
        editText.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View view, MotionEvent event) {
                // 在TOUCH的UP事件中，要保存当前的行下标，因为弹出软键盘后，整个画面会被重画

                // 在getView方法的最后，要根据index和当前的行下标手动为EditText设置焦点

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = holder.getPosition();
                }
                return false;
            }
        });
        editText.setTag(holder.getPosition());
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                int position = (int) editText.getTag();
                String temp = s.toString();
                int posDot = temp.indexOf(".");
                if (posDot > 0) {
                    if (temp.length() - posDot - 1 > 2) {
                        s.delete(posDot + 3, posDot + 4);
                    }
                }
                list.get(position).profit=s.toString().trim();
            }
        });

        editText.clearFocus();

        if (index != -1 && index == holder.getPosition()) {
            // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
            editText.requestFocus();
        }
        //如果hashMap不为空，就设置的editText
        if (list.get(holder.getPosition()).profit != null) {
            editText.setText(list.get(holder.getPosition()).profit);
        } else {
            editText.setText("");
        }
    }


}
